#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表 */

List Reverse( List L );
List Reverse22( List L );

int main()
{
    List L1, L2;
    L1 = Read();
    L2 = Reverse22(L1);
    // l1=null;
    printf("l1: \n");
    Print(L1);
    printf("l2: \n");
    Print(L2);
    printf("end \n");
    return 0;
}

List Read() {
    List p = NULL, head = NULL, tail = NULL;
    int N, X;
    scanf("%d",&N);
    head = p;
    for( ; N > 0; N-- ) {
        scanf("%d",&X);
        p = (List)malloc(sizeof(struct Node));
        p->Data = X;
        p->Next = NULL;
        if (head == NULL) {
            head = p;
        } else {
            // tail = p;
            tail->Next = p;
        }
        tail = p;
    }
    return head;
}

// List Read () {
//     List head = NULL, tail = NULL, p;
//     int N,X;
//     scanf("%d",&N);
//     for ( ; N > 0; N-- ) {
//         scanf("%d",&X);
//         p = (List)malloc(sizeof(struct Node));
//         p->Data = X;
//         p->Next = NULL;
//         if ( head == NULL ) {
//             head = p;
//         } else {
//             tail->Next = p;
//         }
//         tail = p;
//     }
//     return head;
// }

void Print( List L ) {
    List cur;
    while (L) {
        cur = L;
        printf("%d ",L->Data);
        L = L->Next;
        
       free(cur);
    }
    printf("\r\n");
}

// List Reverse( List L ) {
//     List p = L;
//     int tmp[5], i = 0;
//     while(p) {
//         tmp[i++] = p->Data;
//         p = p->Next;
//     }
//     while(L) {
//         L->Data = tmp[--i];
//         L = L->Next;
//     }

//     return L;
// }

List Reverse( List L ) {
    List p = L;
    int *tmp, *PtrToTmp;
    tmp = (int*)malloc(sizeof(int));
    PtrToTmp = tmp;
    while(p) {
        *PtrToTmp = p->Data;
        p = p->Next;
        PtrToTmp++;
        tmp = (int*)malloc(sizeof(int));
        
    }
    while(L) {
        L->Data = *(--PtrToTmp);
        L = L->Next;
    }
    free(tmp);
    return L;
}

List Reverse22( List L ) {
    List p = NULL, head = NULL, tail = NULL;
    head = p;
    while( L ) {
        p = (List)malloc(sizeof(struct Node));
        p->Data = L->Data;
        p->Next = tail;
        tail = p; 
        L = L->Next;
    }
    return p;
}
